package com.xuecheng.base.util;

import com.baomidou.mybatisplus.core.toolkit.AES;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: AES加密工具类
 * @date 2025/1/12 17:37
 */
public class AESUtil {

    /**
     * 密钥
     */
    private static final String KEY;

    // 获取密钥
    static {
        try {
            // 从环境变量中获取密钥
            KEY = System.getenv("AES_KEY");
            if (KEY == null || KEY.isEmpty()) {
                throw new IllegalStateException("Environment variable AES_KEY is not set.");
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize AES key", e);
        }
    }

    /**
     * 加密
     *
     * @param content 内容
     * @return 加密后的内容
     */
    public static String encrypt(String content) {
        return AES.encrypt(content, KEY);
    }

    /**
     * 解密
     *
     * @param content 内容
     * @return 解密后的内容
     */
    public static String decrypt(String content) {
        return AES.decrypt(content, KEY);
    }
}
